package com.example.myprojectoffeixue;

import android.annotation.TargetApi;
import android.app.Activity;
import android.app.Dialog;
import android.app.Service;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Vibrator;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.TimePicker;
import android.widget.Toast;
import com.alibaba.fastjson.JSON;
import com.localization.Gateway;
import com.localization.GatewayRequestUtil;
import com.localization.SendUtil;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Timer;
import java.util.TimerTask;

public class KongTiaoActivity extends Activity {
    private Dialog dialog;
    private int flag1 = 4;
    public MyApplication m;
    public ImageView tv;
    public TextView tv1;
    public LinearLayout tv2;
    public LinearLayout tv3;
    public TextView tv4;
    public TextView tv5;
    public TextView tv6;
    public TextView tv7;
    public ImageView tv10;
    public ImageView tv11;
    public LinearLayout tv14;
    public TextView tv15;
    public LinearLayout tv16;
    public TextView tv17;
    public String powerStatus;
    public String mode;
    public String speed;
    public String temperature;
    public String timerHour;
    public String timerMin;
    public String timerStatus;
    public String timerType;
    public String type;
    private SheBeiClass sb;
    private Integer it;
    private int flag = 0;
    String mflag;
    ArrayList<String> mextra;
    Handler hand = new Handler() {

        public void handleMessage(android.os.Message msg) {

            if (msg.what == 32) {
                mode = m.t.mode;
                speed = m.t.speed;
                temperature = m.t.temperature;
                if (flag == 0) {
                    powerStatus = m.t.powerStatus;
                    it = Integer.decode(temperature);
                    flag = 1;
                }
                timerHour = m.t.timerHour;
                timerMin = m.t.timerMin;
                timerStatus = m.t.timerStatus;
                timerType = m.t.timerType;
                type = m.t.type;
                tv15.setText(temperature);
                //dialog.dismiss();
                if ("00".equals(m.t.powerStatus)) {
                    tv.setImageResource(R.drawable.ktguan);
                } else {
                    tv.setImageResource(R.drawable.start);

                }


                if ("01".equals(mode)) {


                    iv3.setImageResource(R.drawable.dianjizhileng);
                    iv2.setImageResource(R.drawable.zhire);
                    //iv1.setImageResource(R.drawable.tongfeng);


                } else if ("02".equals(mode)) {

                    iv2.setImageResource(R.drawable.dinajizhire);
                    //iv1.setImageResource(R.drawable.tongfeng);
                    iv3.setImageResource(R.drawable.zhileng);


                } else if ("03".equals(mode)) {

                    //iv1.setImageResource(R.drawable.dianjitongfeng);
                    iv2.setImageResource(R.drawable.zhire);
                    iv3.setImageResource(R.drawable.zhileng);
                }


                if ("00".equals(speed)) {
                    l.setBackgroundResource(R.drawable.fengzidong);
                } else if ("01".equals(speed)) {
                    l.setBackgroundResource(R.drawable.disufeng);
                } else if ("02".equals(speed)) {
                    l.setBackgroundResource(R.drawable.zhongsufeng);
                } else if ("03".equals(speed)) {
                    l.setBackgroundResource(R.drawable.gaosufeng);
                }

            }

        }

    };
    private LinearLayout l;
    private ImageView iv1;
    private ImageView iv2;
    private ImageView iv3;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_kong_tiao);
        info();
    }
    private void info() {
        m = (MyApplication) getApplication();
        m.hand = hand;
        sb = new SheBeiClass();


        Intent it2 = getIntent();
        mflag = it2.getStringExtra("flag");
        mextra = it2.getStringArrayListExtra("extra");

        if (mflag == null) {
            sb.ieee = it2.getStringExtra("ieee");
            sb.port = it2.getStringExtra("port");
            sb.mac = it2.getStringExtra("mac");

        } else {
            for (int i = 0; i < mextra.size(); i++) {
                if ("1".equals(mextra.get(i))) {
                    sb.ieee = m.lsb7.get(i).ieee;
                    sb.port = m.lsb7.get(i).port;
                    sb.mac = m.lsb7.get(i).mac;
                    break;
                }
            }

        }

        sb.cmd = "1011";
        sb.status = "06";
        String t = JSON.toJSONString(sb);
        Log.d("tt", t);
        if (!Gateway.MacSocke.containsKey(sb.mac)) {
            if(m.flag3){
            m.c.send(t);}
        }else{
            Log.d("内网发送","内网发送");
            String cmd= GatewayRequestUtil.createCentraAirSearchStr(sb.ieee,sb.port);
            SendUtil.Mac = sb.mac;
            SendUtil.cmd = cmd;
            SendUtil.hand.sendEmptyMessage(0);
        }



        l = (LinearLayout) findViewById(R.id.lin_kongtiao);
        iv1 = (ImageView) findViewById(R.id.dianjitongfeng);
        iv2 = (ImageView) findViewById(R.id.dianjizhire);
        iv3 = (ImageView) findViewById(R.id.iv_lengfeng);
        tv = (ImageView) findViewById(R.id.start_kongtiao);
        tv2 = (LinearLayout) findViewById(R.id.lengfeng_kongtiao);
        tv3 = (LinearLayout) findViewById(R.id.nuanfeng_kongtiao);
        tv4 = (TextView) findViewById(R.id.zidongfeng_kongtiao);
        tv5 = (TextView) findViewById(R.id.digufeng_kongtiao);
        tv6 = (TextView) findViewById(R.id.zhongsu_kongtiao);
        tv7 = (TextView) findViewById(R.id.gaofeng_kongtiao);
        tv10 = (ImageView) findViewById(R.id.addwd_kongtiao);
        tv11 = (ImageView) findViewById(R.id.jswd_kongtiao);
        tv14 = (LinearLayout) findViewById(R.id.tongfeng_kongtiao);
        tv15 = (TextView) findViewById(R.id.wendu_kongtiao);
        tv16 = (LinearLayout) findViewById(R.id.hour_kongtiao);
        LinearLayout iv = (LinearLayout) findViewById(R.id.iv_kongtiao);
        iv.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                finish();
            }
        });

        tv.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {


                sb.extra = new Extra();
                if ("00".equals(powerStatus)) {
                    sb.extra.powerStatus = "01";
                    powerStatus = "01";
                    tv.setImageResource(R.drawable.start);

                } else {
                    sb.extra.powerStatus = "00";
                    powerStatus = "00";
                    tv.setImageResource(R.drawable.ktguan);
                }

                sb.status = "01";
                sb.extra.type = "01";
                sb.extra.mode = mode;
                sb.extra.speed = speed;
                sb.extra.temperature = temperature;
                sb.extra.timerHour = timerHour;
                sb.extra.timerMin = timerMin;
                sb.extra.timerType = timerType;
                sb.extra.timerStatus = timerStatus;
                    if (mflag != null) {
                        for (int i = 0; i < mextra.size(); i++) {
                            if ("1".equals(mextra.get(i))) {
                                sb.ieee = m.lsb7.get(i).ieee;
                                sb.port = m.lsb7.get(i).port;
                                sb.mac = m.lsb7.get(i).mac;
                                String t = JSON.toJSONString(sb);
                                Log.d("cmd4444",t);
                                if (!Gateway.MacSocke.containsKey(sb.mac)) {
                                    if(m.flag3){
                                    m.c.send(t);}
                                }else{
                                    sb.ieee = m.lsb7.get(i).ieee;
                                    sb.port = m.lsb7.get(i).port;
                                    String t2 = JSON.toJSONString(sb.extra);
                                    String cmd = GatewayRequestUtil.createCentraAirStr(sb.ieee,sb.port,t2);
                                    SendUtil.Mac = sb.mac;
                                    SendUtil.cmd = cmd;
                                    SendUtil.hand.sendEmptyMessage(0);
                                }
                            }

                        }

                        Vibrator vibrator = (Vibrator) getSystemService(Service.VIBRATOR_SERVICE);
                        vibrator.vibrate(new long[]{0, 300}, -1);


                    } else {

                        String t = JSON.toJSONString(sb);
                        if (!Gateway.MacSocke.containsKey(sb.mac)) {
                            if(m.flag3){
                            m.c.send(t);}}else{
                            String t2 = JSON.toJSONString(sb.extra);
                            String cmd = GatewayRequestUtil.createCentraAirStr(sb.ieee,sb.port,t2);
                            SendUtil.Mac = sb.mac;
                            SendUtil.cmd = cmd;
                            SendUtil.hand.sendEmptyMessage(0);
                        }

                        Log.d("t", t);
                        Vibrator vibrator = (Vibrator) getSystemService(Service.VIBRATOR_SERVICE);
                        vibrator.vibrate(new long[]{0, 300}, -1);

                    }
                }
        });





        tv2.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                if ("00".equals(powerStatus)) {
                    HttpConnection.showToast(getBaseContext(),"请先开启空调");
                } else {
                    sb.status = "01";
                    sb.extra = new Extra();
                    sb.extra.type = "02";
                    sb.extra.mode = "01";
                    sb.extra.speed = speed;
                    sb.extra.powerStatus = powerStatus;
                    sb.extra.temperature = temperature;
                    sb.extra.timerHour = timerHour;
                    sb.extra.timerMin = timerMin;
                    sb.extra.timerType = timerType;
                    sb.extra.timerStatus = timerStatus;
                    iv3.setImageResource(R.drawable.dianjizhileng);
                    iv2.setImageResource(R.drawable.zhire);
                    //iv1.setImageResource(R.drawable.tongfeng);

                        if (mflag != null) {
                            for (int i = 0; i < mextra.size(); i++) {
                                if ("1".equals(mextra.get(i))) {
                                    sb.ieee = m.lsb7.get(i).ieee;
                                    sb.port = m.lsb7.get(i).port;
                                    sb.mac = m.lsb7.get(i).mac;
                                    String t = JSON.toJSONString(sb);
                                    if (!Gateway.MacSocke.containsKey(sb.mac)) {
                                        if(m.flag3){
                                    m.c.send(t);}}else{
                                        sb.ieee = m.lsb7.get(i).ieee;
                                        sb.port = m.lsb7.get(i).port;
                                        String t2 = JSON.toJSONString(sb.extra);
                                        String cmd = GatewayRequestUtil.createCentraAirStr(sb.ieee,sb.port,t2);
                                        SendUtil.Mac = sb.mac;
                                        SendUtil.cmd = cmd;
                                        SendUtil.hand.sendEmptyMessage(0);
                                    }

                                }
                            }
                            Vibrator vibrator = (Vibrator) getSystemService(Service.VIBRATOR_SERVICE);
                            vibrator.vibrate(new long[]{0, 300}, -1);
                        } else {
                            String t = JSON.toJSONString(sb);
                            if (!Gateway.MacSocke.containsKey(sb.mac)) {
                                if(m.flag3){
                            m.c.send(t);}
                            }else{
                                String t2 = JSON.toJSONString(sb.extra);
                                String cmd = GatewayRequestUtil.createCentraAirStr(sb.ieee,sb.port,t2);
                                SendUtil.Mac = sb.mac;
                                SendUtil.cmd = cmd;
                                SendUtil.hand.sendEmptyMessage(0);
                            }
                            Vibrator vibrator = (Vibrator) getSystemService(Service.VIBRATOR_SERVICE);
                            vibrator.vibrate(new long[]{0, 300}, -1);

                        }

                }


            }
        });

        tv3.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                if ("00".equals(powerStatus)) {
                    HttpConnection.showToast(getBaseContext(),"请先开启空调");


                } else {
                    sb.status = "01";
                    sb.extra = new Extra();
                    sb.extra.type = "02";
                    sb.extra.mode = "02";
                    sb.extra.speed = speed;
                    sb.extra.powerStatus = powerStatus;
                    sb.extra.temperature = temperature;
                    sb.extra.timerHour = timerHour;
                    sb.extra.timerMin = timerMin;
                    sb.extra.timerType = timerType;
                    sb.extra.timerStatus = timerStatus;
                    iv2.setImageResource(R.drawable.dinajizhire);
                    //iv1.setImageResource(R.drawable.tongfeng);
                    iv3.setImageResource(R.drawable.zhileng);
                        if (mflag != null) {
                            for (int i = 0; i < mextra.size(); i++) {
                                if ("1".equals(mextra.get(i))) {
                                    sb.ieee = m.lsb7.get(i).ieee;
                                    sb.port = m.lsb7.get(i).port;
                                    sb.mac = m.lsb7.get(i).mac;
                                    String t = JSON.toJSONString(sb);
                                    if (!Gateway.MacSocke.containsKey(sb.mac)) {
                                        if(m.flag3){
                                    m.c.send(t);}
                                    }else{
                                        sb.ieee = m.lsb7.get(i).ieee;
                                        sb.port = m.lsb7.get(i).port;
                                        String t2 = JSON.toJSONString(sb.extra);
                                        String cmd = GatewayRequestUtil.createCentraAirStr(sb.ieee,sb.port,t2);
                                        SendUtil.Mac = sb.mac;
                                        SendUtil.cmd = cmd;
                                        SendUtil.hand.sendEmptyMessage(0);
                                    }


                                }


                            }
                            Vibrator vibrator = (Vibrator) getSystemService(Service.VIBRATOR_SERVICE);
                            vibrator.vibrate(new long[]{0, 300}, -1);
                        } else {

                            String t = JSON.toJSONString(sb);
                            if (!Gateway.MacSocke.containsKey(sb.mac)) {
                                if(m.flag3){
                            m.c.send(t);}
                            }else{
                                String t2 = JSON.toJSONString(sb.extra);
                                String cmd = GatewayRequestUtil.createCentraAirStr(sb.ieee,sb.port,t2);
                                SendUtil.Mac = sb.mac;
                                SendUtil.cmd = cmd;
                                SendUtil.hand.sendEmptyMessage(0);
                            }
                            Vibrator vibrator = (Vibrator) getSystemService(Service.VIBRATOR_SERVICE);
                            vibrator.vibrate(new long[]{0, 300}, -1);


                        }
                    }
                }



        });


        tv14.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                    sb.status = "01";
                    sb.extra = new Extra();
                    sb.extra.type = "05";
                    sb.extra.mode = mode;
                    sb.extra.speed = speed;
                    sb.extra.powerStatus = powerStatus;
                    sb.extra.temperature = temperature;
                    sb.extra.timerStatus = "00";
                    sb.extra.timerHour = timerHour;
                    sb.extra.timerMin = timerMin;
                    sb.extra.timerType = timerType;

                        if (mflag != null) {
                            for (int i = 0; i < mextra.size(); i++) {
                                if ("1".equals(mextra.get(i))) {
                                    sb.ieee = m.lsb7.get(i).ieee;
                                    sb.port = m.lsb7.get(i).port;
                                    sb.mac = m.lsb7.get(i).mac;
                                    String t = JSON.toJSONString(sb);
                                    if (!Gateway.MacSocke.containsKey(sb.mac)) {
                                        if(m.flag3){
                                    m.c.send(t);}}else {
                                        sb.ieee = m.lsb7.get(i).ieee;
                                        sb.port = m.lsb7.get(i).port;
                                        String t2 = JSON.toJSONString(sb.extra);
                                        String cmd = GatewayRequestUtil.createCentraAirStr(sb.ieee,sb.port,t2);
                                        SendUtil.Mac = sb.mac;
                                        SendUtil.cmd = cmd;
                                        SendUtil.hand.sendEmptyMessage(0);

                                    }

                                }
                            }

                            Vibrator vibrator = (Vibrator) getSystemService(Service.VIBRATOR_SERVICE);
                            vibrator.vibrate(new long[]{0, 300}, -1);


                        } else {
                            String t = JSON.toJSONString(sb);

                            if (!Gateway.MacSocke.containsKey(sb.mac)) {
                                if(m.flag3){
                                m.c.send(t);}}else {
                                String t2 = JSON.toJSONString(sb.extra);
                                String cmd = GatewayRequestUtil.createCentraAirStr(sb.ieee,sb.port,t2);
                                SendUtil.Mac = sb.mac;
                                SendUtil.cmd = cmd;
                                SendUtil.hand.sendEmptyMessage(0);
                            }
                            Vibrator vibrator = (Vibrator) getSystemService(Service.VIBRATOR_SERVICE);
                            vibrator.vibrate(new long[]{0, 300}, -1);


                        }
                    }
        });



//--------------------------------------------------------------


        tv4.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                if ("00".equals(powerStatus)) {
                    HttpConnection.showToast(getBaseContext(),"请先开启空调");

                } else {

                    l.setBackgroundResource(R.drawable.fengzidong);

                    sb.status = "01";
                    sb.extra = new Extra();
                    sb.extra.type = "03";
                    sb.extra.mode = mode;
                    sb.extra.speed = "00";
                    sb.extra.powerStatus = powerStatus;
                    sb.extra.temperature = temperature;
                    sb.extra.timerHour = timerHour;
                    sb.extra.timerMin = timerMin;
                    sb.extra.timerType = timerType;
                    sb.extra.timerStatus = timerStatus;

                        if (mflag != null) {
                            for (int i = 0; i < mextra.size(); i++) {
                                if ("1".equals(mextra.get(i))) {
                                    sb.ieee = m.lsb7.get(i).ieee;
                                    sb.port = m.lsb7.get(i).port;
                                    sb.mac = m.lsb7.get(i).mac;
                                    String t = JSON.toJSONString(sb);
                                    if (!Gateway.MacSocke.containsKey(sb.mac)) {
if(m.flag3) {
    m.c.send(t);
}
                                    }else{
                                        sb.ieee = m.lsb7.get(i).ieee;
                                        sb.port = m.lsb7.get(i).port;
                                        String t2 = JSON.toJSONString(sb.extra);
                                        String cmd = GatewayRequestUtil.createCentraAirStr(sb.ieee,sb.port,t2);
                                        SendUtil.Mac = sb.mac;
                                        SendUtil.cmd = cmd;
                                        SendUtil.hand.sendEmptyMessage(0);

                                    }




                                }
                            }

                            Vibrator vibrator = (Vibrator) getSystemService(Service.VIBRATOR_SERVICE);
                            vibrator.vibrate(new long[]{0, 300}, -1);


                        } else {
                            String t = JSON.toJSONString(sb);
                            if (!Gateway.MacSocke.containsKey(sb.mac)) {
                                if(m.flag3){
                            m.c.send(t);}}else{
                                String t2 = JSON.toJSONString(sb.extra);
                                String cmd = GatewayRequestUtil.createCentraAirStr(sb.ieee,sb.port,t2);
                                SendUtil.Mac = sb.mac;
                                SendUtil.cmd = cmd;
                                SendUtil.hand.sendEmptyMessage(0);
                            }
                            Vibrator vibrator = (Vibrator) getSystemService(Service.VIBRATOR_SERVICE);
                            vibrator.vibrate(new long[]{0, 300}, -1);

                    }
                }

            }


        });


        tv5.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                if ("00".equals(powerStatus)) {
                    HttpConnection.showToast(getBaseContext(),"请先开启空调");
                } else {
                    l.setBackgroundResource(R.drawable.disufeng);
                    sb.status = "01";
                    sb.extra = new Extra();
                    sb.extra.type = "03";
                    sb.extra.mode = mode;
                    sb.extra.speed = "01";
                    sb.extra.powerStatus = powerStatus;
                    sb.extra.temperature = temperature;
                    sb.extra.timerHour = timerHour;
                    sb.extra.timerMin = timerMin;
                    sb.extra.timerType = timerType;
                    sb.extra.timerStatus = timerStatus;


                        if (mflag != null) {
                            for (int i = 0; i < mextra.size(); i++) {
                                if ("1".equals(mextra.get(i))) {
                                    sb.ieee = m.lsb7.get(i).ieee;
                                    sb.port = m.lsb7.get(i).port;
                                    sb.mac = m.lsb7.get(i).mac;
                                    String t = JSON.toJSONString(sb);
                                    if (!Gateway.MacSocke.containsKey(sb.mac)) {
                                        if(m.flag3){
                                            m.c.send(t);}} else {

                                        sb.ieee = m.lsb7.get(i).ieee;
                                        sb.port = m.lsb7.get(i).port;
                                        String t2 = JSON.toJSONString(sb.extra);
                                        String cmd = GatewayRequestUtil.createCentraAirStr(sb.ieee,sb.port,t2);
                                        SendUtil.Mac = sb.mac;
                                        SendUtil.cmd = cmd;
                                        SendUtil.hand.sendEmptyMessage(0);





                                    }

                                }

                            }

                            Vibrator vibrator = (Vibrator) getSystemService(Service.VIBRATOR_SERVICE);
                            vibrator.vibrate(new long[]{0, 300}, -1);


                        } else {
                            String t = JSON.toJSONString(sb);
                            if (!Gateway.MacSocke.containsKey(sb.mac)) {
                                if(m.flag3){
                                    m.c.send(t);}}else{

                                String t2 = JSON.toJSONString(sb.extra);
                                String cmd = GatewayRequestUtil.createCentraAirStr(sb.ieee,sb.port,t2);
                                SendUtil.Mac = sb.mac;
                                SendUtil.cmd = cmd;
                                SendUtil.hand.sendEmptyMessage(0);
                            }
                            Vibrator vibrator = (Vibrator) getSystemService(Service.VIBRATOR_SERVICE);
                            vibrator.vibrate(new long[]{0, 300}, -1);

                        }
                    }
                }

        });

        //--------------------------------------------------------------------
        tv6.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                if ("00".equals(powerStatus)) {
                    HttpConnection.showToast(getBaseContext(),"请先开启空调");
                } else {

                    l.setBackgroundResource(R.drawable.zhongsufeng);
                    sb.status = "01";
                    sb.extra = new Extra();
                    sb.extra.type = "03";
                    sb.extra.mode = mode;
                    sb.extra.speed = "02";
                    sb.extra.powerStatus = powerStatus;
                    sb.extra.temperature = temperature;
                    sb.extra.timerHour = timerHour;
                    sb.extra.timerMin = timerMin;
                    sb.extra.timerType = timerType;
                    sb.extra.timerStatus = timerStatus;

                        if (mflag != null) {
                            for (int i = 0; i < mextra.size(); i++) {
                                if ("1".equals(mextra.get(i))) {
                                    sb.ieee = m.lsb7.get(i).ieee;
                                    sb.port = m.lsb7.get(i).port;
                                    sb.mac = m.lsb7.get(i).mac;
                                    String t = JSON.toJSONString(sb);
                                    if (!Gateway.MacSocke.containsKey(sb.mac)) {
                                        if(m.flag3){
                                            m.c.send(t);}} else {
                                        sb.ieee = m.lsb7.get(i).ieee;
                                        sb.port = m.lsb7.get(i).port;
                                        String t2 = JSON.toJSONString(sb.extra);
                                        String cmd = GatewayRequestUtil.createCentraAirStr(sb.ieee,sb.port,t2);
                                        SendUtil.Mac = sb.mac;
                                        SendUtil.cmd = cmd;
                                        SendUtil.hand.sendEmptyMessage(0);

                                    }

                                }
                            }
                            Vibrator vibrator = (Vibrator) getSystemService(Service.VIBRATOR_SERVICE);
                            vibrator.vibrate(new long[]{0, 300}, -1);


                        } else {
                            String t = JSON.toJSONString(sb);
                            if (!Gateway.MacSocke.containsKey(sb.mac)) {
                                if(m.flag3){
                                    m.c.send(t);}}else{
                                String t2 = JSON.toJSONString(sb.extra);
                                String cmd = GatewayRequestUtil.createCentraAirStr(sb.ieee,sb.port,t2);
                                SendUtil.Mac = sb.mac;
                                SendUtil.cmd = cmd;
                                SendUtil.hand.sendEmptyMessage(0);
                            }
                            Vibrator vibrator = (Vibrator) getSystemService(Service.VIBRATOR_SERVICE);
                            vibrator.vibrate(new long[]{0, 300}, -1);

                        }
                    }
                }

        });


        //------------------------------------------------------------------------

        tv7.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                if ("00".equals(powerStatus)) {
                    HttpConnection.showToast(getBaseContext(),"请先开启空调");
                } else {

                    l.setBackgroundResource(R.drawable.gaosufeng);
                    sb.status = "01";
                    sb.extra = new Extra();
                    sb.extra.type = "03";
                    sb.extra.mode = mode;
                    sb.extra.speed = "03";
                    sb.extra.powerStatus = powerStatus;
                    sb.extra.temperature = temperature;
                    sb.extra.timerHour = timerHour;
                    sb.extra.timerMin = timerMin;
                    sb.extra.timerType = timerType;
                    sb.extra.timerStatus = timerStatus;

                        if (mflag != null) {
                            for (int i = 0; i < mextra.size(); i++) {
                                if ("1".equals(mextra.get(i))) {
                                    sb.ieee = m.lsb7.get(i).ieee;
                                    sb.port = m.lsb7.get(i).port;
                                    sb.mac = m.lsb7.get(i).mac;
                                    String t = JSON.toJSONString(sb);
                                    if (!Gateway.MacSocke.containsKey(sb.mac)) {
                                        if(m.flag3){
                                            m.c.send(t);}}else{


                                        sb.ieee = m.lsb7.get(i).ieee;
                                        sb.port = m.lsb7.get(i).port;
                                        String t2 = JSON.toJSONString(sb.extra);
                                        String cmd = GatewayRequestUtil.createCentraAirStr(sb.ieee,sb.port,t2);
                                        SendUtil.Mac = sb.mac;
                                        SendUtil.cmd = cmd;
                                        SendUtil.hand.sendEmptyMessage(0);




                                    }
                            }
                            }
                            Vibrator vibrator = (Vibrator) getSystemService(Service.VIBRATOR_SERVICE);
                            vibrator.vibrate(new long[]{0, 300}, -1);
                        } else {
                            String t = JSON.toJSONString(sb);
                            if (!Gateway.MacSocke.containsKey(sb.mac)) {
                                if(m.flag3){
                                    m.c.send(t);}}else{
                                String t2 = JSON.toJSONString(sb.extra);
                                String cmd = GatewayRequestUtil.createCentraAirStr(sb.ieee,sb.port,t2);
                                SendUtil.Mac = sb.mac;
                                SendUtil.cmd = cmd;
                                SendUtil.hand.sendEmptyMessage(0);
                            }
                            Vibrator vibrator = (Vibrator) getSystemService(Service.VIBRATOR_SERVICE);
                            vibrator.vibrate(new long[]{0, 300}, -1);

                        }
                    }
            }
        });



        tv10.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                if (it != null) {
                    if ("00".equals(powerStatus)) {
                        HttpConnection.showToast(getBaseContext(),"请先开启空调");
                    } else {
                        it++;
                        if (it > 35) {
                            it = 35;
                        }
                        if (it < 10) {
                            tv15.setText("  " + it.toString());
                        }
                        tv15.setText(it.toString());

                        sb.status = "01";
                        sb.extra = new Extra();
                        sb.extra.type = "04";
                        sb.extra.mode = mode;
                        sb.extra.speed = speed;
                        sb.extra.powerStatus = powerStatus;
                        sb.extra.temperature = it.toString();
                        sb.extra.timerHour = timerHour;
                        sb.extra.timerMin = timerMin;
                        sb.extra.timerType = timerType;
                        sb.extra.timerStatus = timerStatus;

                            if (mflag != null) {
                                for (int i = 0; i < mextra.size(); i++) {
                                    if ("1".equals(mextra.get(i))) {
                                        sb.ieee = m.lsb7.get(i).ieee;
                                        sb.port = m.lsb7.get(i).port;
                                        sb.mac = m.lsb7.get(i).mac;
                                        String t = JSON.toJSONString(sb);
                                        if (!Gateway.MacSocke.containsKey(sb.mac)) {
                                            if(m.flag3){
                                                m.c.send(t);}} else {
                                            sb.ieee = m.lsb7.get(i).ieee;
                                            sb.port = m.lsb7.get(i).port;
                                            String t2 = JSON.toJSONString(sb.extra);
                                            String cmd = GatewayRequestUtil.createCentraAirStr(sb.ieee,sb.port,t2);
                                            SendUtil.Mac = sb.mac;
                                            SendUtil.cmd = cmd;
                                            SendUtil.hand.sendEmptyMessage(0);
                                        }
                                    }

                                }
                                Vibrator vibrator = (Vibrator) getSystemService(Service.VIBRATOR_SERVICE);
                                vibrator.vibrate(new long[]{0, 300}, -1);


                            } else {
                                String t = JSON.toJSONString(sb);
                                if (!Gateway.MacSocke.containsKey(sb.mac)) {
                                    if(m.flag3){
                                        m.c.send(t);}}else{
                                    String t2 = JSON.toJSONString(sb.extra);
                                    String cmd = GatewayRequestUtil.createCentraAirStr(sb.ieee,sb.port,t2);
                                    SendUtil.Mac = sb.mac;
                                    SendUtil.cmd = cmd;
                                    SendUtil.hand.sendEmptyMessage(0);
                                }
                                Vibrator vibrator = (Vibrator) getSystemService(Service.VIBRATOR_SERVICE);
                                vibrator.vibrate(new long[]{0, 300}, -1);
                            }
                        }
                    }
                }

        });

        //--------------------------------------------------------------
        tv11.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                if (it != null) {
                    if ("00".equals(powerStatus)) {
                        HttpConnection.showToast(getBaseContext(),"请先开启空调");
                    } else {

                        it--;
                        if (it < 5) {
                            it = 5;
                        }
                        if (it < 10) {
                            tv15.setText("  " + it.toString());

                        }
                        tv15.setText(it.toString());
                        sb.status = "01";
                        sb.extra = new Extra();
                        sb.extra.type = "04";
                        sb.extra.mode = mode;
                        sb.extra.speed = speed;
                        sb.extra.powerStatus = powerStatus;

                        sb.extra.temperature = it.toString();
                        sb.extra.timerHour = timerHour;
                        sb.extra.timerMin = timerMin;
                        sb.extra.timerType = timerType;
                        sb.extra.timerStatus = timerStatus;


                            if (mflag != null) {
                                for (int i = 0; i < mextra.size(); i++) {
                                    if ("1".equals(mextra.get(i))) {
                                        sb.ieee = m.lsb7.get(i).ieee;
                                        sb.port = m.lsb7.get(i).port;
                                        sb.mac = m.lsb7.get(i).mac;
                                        String t = JSON.toJSONString(sb);
                                        if (!Gateway.MacSocke.containsKey(sb.mac)) {
                                            if(m.flag3){
                                                m.c.send(t);}}else{
                                            sb.ieee = m.lsb7.get(i).ieee;
                                            sb.port = m.lsb7.get(i).port;
                                            String t2 = JSON.toJSONString(sb.extra);
                                            String cmd = GatewayRequestUtil.createCentraAirStr(sb.ieee,sb.port,t2);
                                            SendUtil.Mac = sb.mac;
                                            SendUtil.cmd = cmd;
                                            SendUtil.hand.sendEmptyMessage(0);
                                        }
                                }

                                }
                                Vibrator vibrator = (Vibrator) getSystemService(Service.VIBRATOR_SERVICE);
                                vibrator.vibrate(new long[]{0, 300}, -1);

                            } else {
                                String t = JSON.toJSONString(sb);
                                if (!Gateway.MacSocke.containsKey(sb.mac)) {
                                    if(m.flag3){
                                        m.c.send(t);}}else{
                                    String t2 = JSON.toJSONString(sb.extra);
                                    String cmd = GatewayRequestUtil.createCentraAirStr(sb.ieee,sb.port,t2);
                                    SendUtil.Mac = sb.mac;
                                    SendUtil.cmd = cmd;
                                    SendUtil.hand.sendEmptyMessage(0);
                                }

                                Vibrator vibrator = (Vibrator) getSystemService(Service.VIBRATOR_SERVICE);
                                vibrator.vibrate(new long[]{0, 300}, -1);


                            }
                        }
                    }


                }



        });



        tv16.setOnClickListener(new OnClickListener() {

            private TimePicker tp;
            @Override
            public void onClick(View v) {
                flag1=4;
                sb.status = "01";
                sb.extra = new Extra();
                sb.extra.type = "05";
                sb.extra.mode = mode;
                sb.extra.speed = speed;
                sb.extra.powerStatus = powerStatus;
                sb.extra.temperature = temperature;
                dialog = new Dialog(KongTiaoActivity.this);
                dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
                dialog.setContentView(R.layout.dingshidialog);
                     ImageView iv1 = (ImageView) dialog.findViewById(R.id.delete_dingshi);
                 tp = (TimePicker) dialog.findViewById(R.id.timepicker);
                 tp.setIs24HourView(true);
                 tp.setCurrentHour(00);
                 tp.setCurrentMinute(00);
                tp.setDescendantFocusability(TimePicker.FOCUS_BLOCK_DESCENDANTS);
                iv1.setOnClickListener(new OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        dialog.dismiss();

                    }
                });
                RadioButton ra = (RadioButton) dialog.findViewById(R.id.radio1_dingshi);
                RadioButton ra1 = (RadioButton) dialog.findViewById(R.id.radio2_dingshi);
                TextView iv = (TextView) dialog.findViewById(R.id.iv_dingshidialog);
                ra.setOnClickListener(new OnClickListener() {

                    @Override
                    public void onClick(View v) {

                        flag1 = 4;
                    }
                });


                ra1.setOnClickListener(new OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        flag1 = 5;
                    }
                });


                iv.setOnClickListener(new OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        if (flag1 == 4) {
                            sb.extra.timerType = "01";

                        } else if (flag1 == 5) {
                            sb.extra.timerType = "00";

                        }
                        sb.extra.timerStatus = "01";
                        if (tp.getCurrentHour() < 10) {
                            sb.extra.timerHour = "0" + tp.getCurrentHour();
                        } else {
                            sb.extra.timerHour = "" + tp.getCurrentHour();
                        }
                        if (tp.getCurrentMinute() < 10) {
                            sb.extra.timerMin = "0" + tp.getCurrentMinute();
                        } else {
                            sb.extra.timerMin = "" + tp.getCurrentMinute();
                        }
                            if (mflag != null) {
                                for (int i = 0; i < mextra.size(); i++) {
                                    if ("1".equals(mextra.get(i))) {
                                        sb.ieee = m.lsb7.get(i).ieee;
                                        sb.port = m.lsb7.get(i).port;
                                        sb.mac = m.lsb7.get(i).mac;
                                        String t = JSON.toJSONString(sb);
                                        if (!Gateway.MacSocke.containsKey(sb.mac)) {
                                            if(m.flag3){
                                                m.c.send(t);}} else {
                                            sb.ieee = m.lsb7.get(i).ieee;
                                            sb.port = m.lsb7.get(i).port;
                                            String t2 = JSON.toJSONString(sb.extra);
                                            String cmd = GatewayRequestUtil.createCentraAirStr(sb.ieee,sb.port,t2);
                                            SendUtil.Mac = sb.mac;
                                            SendUtil.cmd = cmd;
                                            SendUtil.hand.sendEmptyMessage(0);
                                        }
                                        dialog.dismiss();
                                    }
                                }
                            } else {
                                String t = JSON.toJSONString(sb);
                                if (!Gateway.MacSocke.containsKey(sb.mac)) {
                                    if(m.flag3){
                                        m.c.send(t);}}else{

                                    String t2 = JSON.toJSONString(sb.extra);
                                    String cmd = GatewayRequestUtil.createCentraAirStr(sb.ieee,sb.port,t2);
                                    SendUtil.Mac = sb.mac;
                                    SendUtil.cmd = cmd;
                                    SendUtil.hand.sendEmptyMessage(0);
                                }
                                dialog.dismiss();
                        }
                    }
                });

                dialog.show();


            }
        });
    }


    @Override
    protected void onStart() {
        mode = m.t.mode;
        speed = m.t.speed;
        temperature = m.t.temperature;
        if (flag == 0) {
            powerStatus = m.t.powerStatus;
            it = Integer.decode(temperature);
            flag = 1;
        }
        timerHour = m.t.timerHour;
        timerMin = m.t.timerMin;
        timerStatus = m.t.timerStatus;
        timerType = m.t.timerType;
        type = m.t.type;
        tv15.setText(temperature);
        //dialog.dismiss();
        if ("00".equals(m.t.powerStatus)) {
            tv.setImageResource(R.drawable.ktguan);
        } else {
            tv.setImageResource(R.drawable.start);

        }


        if ("01".equals(mode)) {
            iv3.setImageResource(R.drawable.dianjizhileng);
            iv2.setImageResource(R.drawable.zhire);
            //iv1.setImageResource(R.drawable.tongfeng);
        } else if ("02".equals(mode)) {
            iv2.setImageResource(R.drawable.dinajizhire);
            //iv1.setImageResource(R.drawable.tongfeng);
            iv3.setImageResource(R.drawable.zhileng);


        } else if ("03".equals(mode)) {

            //iv1.setImageResource(R.drawable.dianjitongfeng);
            iv2.setImageResource(R.drawable.zhire);
            iv3.setImageResource(R.drawable.zhileng);
        }


        if ("00".equals(speed)) {
            l.setBackgroundResource(R.drawable.fengzidong);

        } else if ("01".equals(speed)) {

            l.setBackgroundResource(R.drawable.disufeng);

        } else if ("02".equals(speed)) {
            l.setBackgroundResource(R.drawable.zhongsufeng);

        } else if ("03".equals(speed)) {
            l.setBackgroundResource(R.drawable.gaosufeng);
        }
        super.onStart();
    }




}



